package com.walnut.dao.impl;

import com.walnut.dao.FoodDao;
import com.walnut.pojo.Food;
import com.walnut.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class FoodDaoImpl implements FoodDao {
    @Override
    public List<Food> select() throws SQLException{
        List<Food> list = new ArrayList<>();
        Connection conn = DBUtil.getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = " select * from food ";
        pstmt = conn.prepareStatement(sql);
        rs = pstmt.executeQuery();
        while (rs.next()) {
        Food food = new Food().builder()
                    .foodId(rs.getInt("food_id"))
                    .foodName(rs.getString("food_name"))
                    .foodImg(rs.getString("food_img"))
                    .price(rs.getDouble("price"))
                    .foodStatus(rs.getString("food_status"))
                    .build();
            list.add(food);
        }
        return list;
    }

    @Override
    public Food selectById(Integer foodId) throws SQLException{
        Connection conn = DBUtil.getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = " select * from food where food_id=?";
        pstmt = conn.prepareStatement(sql);
        pstmt.setInt(1, foodId);
        rs = pstmt.executeQuery();
        rs.next();
        Food food = new Food().builder()
                .foodId(rs.getInt("food_id"))
                .foodName(rs.getString("food_name"))
                .foodImg(rs.getString("food_img"))
                .price(rs.getDouble("price"))
                .foodStatus(rs.getString("food_status"))
                .build();
        return food;
    }

    public int selectByName(String foodName) throws SQLException{
        Connection conn = DBUtil.getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        String sql = " select * from food where food_name=?";
        pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, foodName);
        rs = pstmt.executeQuery();
        if (rs.next()) return 1;
        else return 0;
    }

    public List<Food> selectByConditions(String foodName, String price, String foodStatus) throws SQLException {
        List<Food> foodList = new ArrayList<>();
        StringBuilder sql = new StringBuilder("SELECT * FROM food WHERE 1=1");

        // 构建查询条件
        if (foodName != null && !foodName.isEmpty()) {
            sql.append(" AND food_name LIKE ?");
        }
        if (price != null && !price.isEmpty()) {
            sql.append(" AND price = ?");
        }
        if (foodStatus != null && !foodStatus.isEmpty()) {
            sql.append(" AND food_status = ?");
        }

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql.toString())) {

            int index = 1;

            // 设置查询参数
            if (foodName != null && !foodName.isEmpty()) {
                stmt.setString(index++, "%" + foodName + "%"); // 模糊匹配食品名称
            }
            if (price != null && !price.isEmpty()) {
                stmt.setDouble(index++, Double.parseDouble(price)); // 精确匹配价格
            }
            if (foodStatus != null && !foodStatus.isEmpty()) {
                stmt.setString(index++, foodStatus); // 精确匹配食品状态
            }

            ResultSet rs = stmt.executeQuery();

            // 判断结果集是否为空
            while (rs.next()) {
                Food food = new Food().builder()
                        .foodId(rs.getInt("food_id"))
                        .foodName(rs.getString("food_name"))
                        .foodImg(rs.getString("food_img"))
                        .price(rs.getDouble("price"))
                        .foodStatus(rs.getString("food_status"))
                        .build();
                foodList.add(food);
            }
            return foodList;
        }

    }


    @Override
    public int insert(Food food) throws SQLException{
        Connection conn = DBUtil.getConnection();
        PreparedStatement pstmt = null;
        String sql = "insert into food (food_name,food_img,price,food_status) values (?,?,?,?)";

        pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, food.getFoodName());
        pstmt.setString(2, food.getFoodImg());
        pstmt.setDouble(3, food.getPrice());
        pstmt.setString(4, food.getFoodStatus());
        int count=pstmt.executeUpdate();
        return count;
    }

    @Override
    public int update(Food food) throws SQLException{
        Connection conn = DBUtil.getConnection();
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        String sql = "update food set food_name=?,food_img=?,price=?,food_status=? where food_id=?";

        pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, food.getFoodName());
        pstmt.setString(2, food.getFoodImg());
        pstmt.setDouble(3, food.getPrice());
        pstmt.setString(4, food.getFoodStatus());
        pstmt.setInt(5, food.getFoodId());
        int count=pstmt.executeUpdate();
        return count;
    }

    @Override
    public int delete(Integer foodId) throws SQLException{
        Connection conn = DBUtil.getConnection();
        PreparedStatement pstmt = null;

        String sql = "delete from food where food_id=?";
        pstmt = conn.prepareStatement(sql);

        pstmt.setInt(1, foodId);
        int count=pstmt.executeUpdate();
        return count;
    }
}
